// Copyright 叶湘 ( weixin:yexiang841, email:yexiang841@qq.com ), Shitouren Studio. All Rights Reserved.

#include "Logger/Q3D_CPPU_Logger.h"
#include "Misc/DateTime.h"

DEFINE_LOG_CATEGORY(__Q3D__);

void UQ3D_CPPU_Logger::Q3D_F_PrintLog(EQ3D_CPPE_LogLevel enum_loglevel, const FString &string_msg, const FString &string_log_source)
{
    FDateTime datetime_now_tmp = FDateTime::UtcNow();
    // FString datetime_now_formatted = FString::Printf( TEXT("%04i-%02i-%02i %02i:%02i:%02i.%03i"), datetime_now_tmp.GetYear(), datetime_now_tmp.GetMonth(), datetime_now_tmp.GetDay(), datetime_now_tmp.GetHour(), datetime_now_tmp.GetMinute(), datetime_now_tmp.GetSecond(), datetime_now_tmp.GetMillisecond());
    FString string_now_formatted = FString::Printf(TEXT("%02i:%02i:%02i.%03i"), datetime_now_tmp.GetHour(), datetime_now_tmp.GetMinute(), datetime_now_tmp.GetSecond(), datetime_now_tmp.GetMillisecond());
    FString string_log = "[" + string_now_formatted + "] " + string_log_source + ": " + string_msg;
    UQ3D_CPPU_Logger::_F_PrintLog(enum_loglevel, string_log);
}

void UQ3D_CPPU_Logger::_F_PrintLog(EQ3D_CPPE_LogLevel enum_loglevel, const FString &string_log)
{
    if (enum_loglevel >= EQ3D_CPPE_LogLevel::Fatal)
    {
        // if( Bool_Print_To_Console ){ UE_LOG( __Q3D__, Fatal, TEXT( "%s" ), *string_log ); };
        if (Bool_Print_To_Screen)
        {
            if (GEngine)
            {
                GEngine->AddOnScreenDebugMessage(-1, UQ3D_CPPU_Logger::Float_Log_Duration_Config, FColor::Black, string_log);
            }
        };
        return;
    }
    if (enum_loglevel < Enum_LogLevel_Config)
    {
        return;
    }
    switch (enum_loglevel)
    {
    // 只使用 5 个级别
    // 打印到界面会使用不同的颜色，保持跟 Q3D_BPU_Logger 一致
    case EQ3D_CPPE_LogLevel::Error:
        if (Bool_Print_To_Console)
        {
            UE_LOG(__Q3D__, Error, TEXT("%s"), *string_log);
        };
        if (Bool_Print_To_Screen)
        {
            if (GEngine)
            {
                GEngine->AddOnScreenDebugMessage(-1, UQ3D_CPPU_Logger::Float_Log_Duration_Config, FColor::Red, string_log);
            }
        };
        break;
    case EQ3D_CPPE_LogLevel::Warning:
        if (Bool_Print_To_Console)
        {
            UE_LOG(__Q3D__, Warning, TEXT("%s"), *string_log);
        };
        if (Bool_Print_To_Screen)
        {
            if (GEngine)
            {
                GEngine->AddOnScreenDebugMessage(-1, UQ3D_CPPU_Logger::Float_Log_Duration_Config, FColor::Yellow, string_log);
            }
        };
        break;
    case EQ3D_CPPE_LogLevel::Display:
        if (Bool_Print_To_Console)
        {
            UE_LOG(__Q3D__, Display, TEXT("%s"), *string_log);
        };
        if (Bool_Print_To_Screen)
        {
            if (GEngine)
            {
                GEngine->AddOnScreenDebugMessage(-1, UQ3D_CPPU_Logger::Float_Log_Duration_Config, FColor::Green, string_log);
            }
        };
        break;
    case EQ3D_CPPE_LogLevel::Log:
        if (Bool_Print_To_Console)
        {
            UE_LOG(__Q3D__, Log, TEXT("Log: %s"), *string_log);
        };
        if (Bool_Print_To_Screen)
        {
            if (GEngine)
            {
                GEngine->AddOnScreenDebugMessage(-1, UQ3D_CPPU_Logger::Float_Log_Duration_Config, FColor::Silver, string_log);
            }
        };
        break;
    case EQ3D_CPPE_LogLevel::Verbose:
        if (Bool_Print_To_Console)
        {
            UE_LOG(__Q3D__, Verbose, TEXT("Log: %s"), *string_log);
        };
        if (Bool_Print_To_Screen)
        {
            if (GEngine)
            {
                GEngine->AddOnScreenDebugMessage(-1, UQ3D_CPPU_Logger::Float_Log_Duration_Config, FColor::Blue, string_log);
            }
        };
        break;
    default:;
    }
    return;
}